/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.


   Akey-GPIO13  Bkey-GPIO12  Ckey-GPIO14
*/
#include "task_public.h"


static const char *TAG = "APP_MAIN";


char *APP_VER = "V1.0.1";

uint8_t esp_mac[6] = {0};


QueueHandle_t QueueHandle_0;
void app_main()
{
	uart_set_baudrate(UART_NUM_0, 921600);	//HLW8110
	uart_set_baudrate(UART_NUM_1, 921600);	//LOG
uart_driver_install(UART_NUM_0, 256, 256, 256, &QueueHandle_0, 0);
	ESP_LOGI(TAG, "NTP TIME");

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is ESP8266 chip with %d CPU cores, WiFi, ",
            chip_info.cores);

    printf("silicon revision %d, ", chip_info.revision);

    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    esp_read_mac(esp_mac, ESP_MAC_WIFI_STA);    

    ESP_LOGI(TAG, "===============");
    ESP_LOGI(TAG, APP_VER);
    ESP_LOGI(TAG, "===============");

#if (1)
    task_nvs_init();
    //ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    wifi_init();

#else

	ESP_ERROR_CHECK(nvs_flash_init());
	ESP_ERROR_CHECK(esp_netif_init());
	ESP_ERROR_CHECK(esp_event_loop_create_default());

	ESP_ERROR_CHECK(example_connect());
#endif

    I2C_Master_Init();

    //task_fram_init();
    //task_led_init();
    //task_oled12864_init();
    //task_hlw8110_init();
    //task_relay_init();
    //task_adc_init();
    task_button_init();
    //task_others_init();
    task_tm1680_init();

    xTaskCreate(sys_rst_task, "sys_rst_task", 256, NULL, 10, NULL);
    xTaskCreate(wifi_task, "wifi_task", 2048, NULL, 9, NULL);
    xTaskCreate(tm1680_task, "tm1680_task", 2048, NULL, 4, NULL);
    //xTaskCreate(led_task, "led_task", configMINIMAL_STACK_SIZE, NULL, 4, NULL);
    //xTaskCreate(oled12864_task, "oled_task", 2048, NULL, 4, NULL);
    //xTaskCreate(hlw8110_task, "hlw_task", configMINIMAL_STACK_SIZE, NULL, 4, NULL);
    //xTaskCreate(adc_task, "adc_task", 1024, NULL, 5, NULL);
    xTaskCreate(button_task, "button_task", 1024, NULL, 5, NULL);
    //xTaskCreate(others_task, "others_task", configMINIMAL_STACK_SIZE, NULL, 4, NULL);
    xTaskCreate(ntp_task, "ntp_task", 2048, NULL, 10, NULL);
    //xTaskCreate(weather_task, "weather_task", 2048, NULL, 10, NULL);

    Start_WebServer();
}
